<?php namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\Wechat\config;
use App\Http\Services\Wechat\WechatLogin;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Support\Collection;
use EasyWeChat\Payment\Order;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;

class WechatController extends Controller
{
    public function index(){
        $app    = app("wechat");
        $server = $app->server;
        $user = $app->user;
        $menu = $app->menu;
        $server->setMessageHandler(function($message) use ($user) {
            $fromUser = $user->get($message->FromUserName);
            $openid   = $message->FromUserName;
            switch($message->MsgType){
                case 'event':
                    if($message->Event == "CLICK"){
                        $eventKey = $message->EventKey;
                        if($eventKey == 'building'){
                            return "即将开放该功能，敬请期待哦！";
                        }
                    }
                    break;
                default:
                    return "{$fromUser->nickname} 您好，感谢您对波罗鸡的关注o(∩_∩)o ";
                    break;

            }
        });

        $buttons =[
            [
                "name" => "遇见波罗鸡",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "波罗鸡由来",
                        "url"  => route('Home.Page.youlai'),
                    ],
                    [
                        "type" => "view",
                        "name" => "波罗鸡定制",
                        "url"  => route('Home.Page.dingzhi'),
                    ],
                    [
                        "type" => "view",
                        "name" => "波罗鸡商城",
                        "url"  => route('Home.Goods.goodsListCash'),
                    ],
                    [
                        "type" => "view",
                        "name" => "呼叫波罗鸡",
                        "url"  => route('Home.Page.hujiao'),
                    ],
                    [
                        "type" => "view",
                        "name" => "我的百宝箱",
                        "url"  => route('Home.User.baibaoxiang'),
                    ],
                ]
            ],
            [
                "type" => "view",
                "name" => "好运马上来",
                "url"  => route('Home.Goods.goodsList'),
            ]
        ];
        $menu->add($buttons);
        $a = $server->serve();
        return $a;

    }

    public function getJsApiPacket(Request $request){
        $info = WechatLogin::getTicketByAccessToken();
        $jsapiTicket = $info->getTicket();

        $nonceStr = WechatLogin::createNonceStr();

        $timestamp = time();

        $url = urldecode($request->get('url'));

        $config = new config();
        $appid  = $config->getAppid();

        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $appid,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return response()->json(['level'=>'success','data'=>$signPackage]);
    }
}